package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.BrokerageConfig;
import org.summer.lawyer.domain.bo.brokerageconfig.BrokerageConfigBo;
import org.summer.lawyer.domain.vo.brokerageconfig.BrokerageConfigVo;
import org.summer.lawyer.mapper.BrokerageConfigMapper;
import org.summer.lawyer.service.IBrokerageConfigService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 分销配置Service业务层处理
 *
 * @author summer
 * @date 2024-06-20
 */
@RequiredArgsConstructor
@Service
public class BrokerageConfigServiceImpl extends ServiceImpl<BrokerageConfigMapper, BrokerageConfig>  implements IBrokerageConfigService {


    /**
     * 查询分销配置
     *
     * @param id 主键
     * @return 分销配置
     */
    @Override
    public BrokerageConfigVo selectBrokerageConfigVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询分销配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 分销配置分页列表
     */
    @Override
    public TableDataInfo<BrokerageConfigVo> selectPageList(BrokerageConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BrokerageConfig> lqw = buildQueryWrapper(bo);
        Page<BrokerageConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的分销配置列表
     *
     * @param bo 查询条件
     * @return 分销配置列表
     */
    @Override
    public List<BrokerageConfigVo> selectBrokerageConfigVoList(BrokerageConfigBo bo) {
        LambdaQueryWrapper<BrokerageConfig> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BrokerageConfig> buildQueryWrapper(BrokerageConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BrokerageConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getEnableBrokerage()), BrokerageConfig::getEnableBrokerage, bo.getEnableBrokerage());
        lqw.eq(StrUtil.isNotBlank(bo.getBrokerageMode()), BrokerageConfig::getBrokerageMode, bo.getBrokerageMode());
        lqw.eq(StrUtil.isNotBlank(bo.getBindMode()), BrokerageConfig::getBindMode, bo.getBindMode());
        lqw.eq(bo.getFirstPercent() != null, BrokerageConfig::getFirstPercent, bo.getFirstPercent());
        lqw.eq(bo.getWithdrawMinPrice() != null, BrokerageConfig::getWithdrawMinPrice, bo.getWithdrawMinPrice());
        lqw.eq(bo.getWithdrawFeePercent() != null, BrokerageConfig::getWithdrawFeePercent, bo.getWithdrawFeePercent());
        lqw.eq(bo.getBrokerageFrozenDay() != null, BrokerageConfig::getBrokerageFrozenDay, bo.getBrokerageFrozenDay());
        lqw.eq(StrUtil.isNotBlank(bo.getWithdrawType()), BrokerageConfig::getWithdrawType, bo.getWithdrawType());
        return lqw;
    }

    /**
     * 新增分销配置
     *
     * @param bo 分销配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BrokerageConfigBo bo) {
        BrokerageConfig add = MapstructUtils.convert(bo, BrokerageConfig.class);
        add.setBrokerageMode("1");
        add.setWithdrawType("1");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分销配置
     *
     * @param bo 分销配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BrokerageConfigBo bo) {
        BrokerageConfig update = MapstructUtils.convert(bo, BrokerageConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BrokerageConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除分销配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public BrokerageConfigVo selectOneBrokerageConfigVo() {
        List<BrokerageConfigVo> list = baseMapper.selectVoList();
        if(CollUtil.isNotEmpty(list)){
            return list.getFirst();
        }
        return null;
    }
}
